package com.pojo.Dao;

import com.pojo.student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.util.List;
@Repository("stuimp")
public class stuimp implements stuinter {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public int add(student s) {
        String sql ="insert into student(id,Name,age,livearea) values(?,?,?,?)";
        Object [] obj=new Object[]{
                s.getId(),
                s.getName(),
                s.getAge(),
                s.getLivearea(),
        };
        int num=this.jdbcTemplate.update(sql,obj);
        return num;
    }

    public int delete(int id) {
        String sql="delete from student where id=?";
        int num=this.jdbcTemplate.update(sql,id);
        return num;

    }

    public int update(student s) {
        String sql="update student set Name=? where id=?";
        Object[] obj=new Object[]{
                s.getId(),
                s.getName(),
        };
        int num=this.jdbcTemplate.update(sql,obj);
        return  num;
    }

    public student findbyid(int id) {
        String sql="select * from student where id=?";
        student stu=this.jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<student>(student.class),id);
        return  stu;
    }

    public List<student> findAll() {
        String sql="select * from student";
        RowMapper<student> rowMapper=new BeanPropertyRowMapper<student>(student.class);
        return this.jdbcTemplate.query(sql, rowMapper);
    }
}
